package cn.tedu.antconlonysever.contract.service.impl;

import cn.tedu.antcolonycommons.ex.ServiceException;
import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.ServiceCode;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IOrderDetailsRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.OrderDetails;
import cn.tedu.antconlonysever.contract.pojo.param.OrderDetailsAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.OrderDetailsUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.OrderDetailsListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.OrderDetailsStandardVO;
import cn.tedu.antconlonysever.contract.service.IOrderDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
@Slf4j
public class OrderDetailsServiceImpl implements IOrderDetailsService  {
    @Value("${antcolony.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IOrderDetailsRepository orderDetailsRepository;

    @Override
    public void addNew(OrderDetailsAddNewParam orderDetailsAddNewParam) {
        log.debug("开始处理【添加订单详情】的业务，参数：{}", orderDetailsAddNewParam);
        Long orderId = orderDetailsAddNewParam.getOrderId();
        int count = orderDetailsRepository.countOrderId(orderId);
        if(count>0){
            String message = "添加订单详情失败，订单id已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        OrderDetails orderDetails=new OrderDetails();
        BeanUtils.copyProperties(orderDetailsAddNewParam,orderDetails);
        //获取数量
        BigDecimal bigDecimal = new BigDecimal(orderDetailsAddNewParam.getGoodsNum());
        //获取单价
        BigDecimal price = orderDetails.getPrice();
        //得到总价
        BigDecimal amount = price.multiply(bigDecimal);
        orderDetails.setAmount(amount);
        orderDetails.setIsValid(1);
        orderDetails.setGmtCreate(LocalDateTime.now());
        orderDetails.setGmtModified(LocalDateTime.now());

        int rows = orderDetailsRepository.insert(orderDetails);
        if(rows!=1){
            String message = "添加订单详情失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除订单详情】的业务，参数：{}", id);
        OrderDetailsStandardVO standardById = orderDetailsRepository.getStandardById(id);
        if(standardById==null){
            String message = "删除订单详情失败，订单详情ID不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        int rows = orderDetailsRepository.deleteById(id);
        if(rows!=1){
            String message = "删除订单详情失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

    }

    @Override
    public void updateInfoById(Long id, OrderDetailsUpdateParam orderDetailsUpdateParam) {
        log.debug("开始处理【修改订单详情】的业务，ID：{}，新数据：{}", id, orderDetailsUpdateParam);

        OrderDetailsStandardVO standardById = orderDetailsRepository.getStandardById(id);
        if(standardById==null){
            String message = "修改订单详情失败，订单详情数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int count = orderDetailsRepository.countOrderIdNotId(id, orderDetailsUpdateParam.getOrderId());
        if(count>0){
            String message = "修改订单详情失败，尝试修改的订单编号已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        OrderDetails orderDetails = new OrderDetails();
        BeanUtils.copyProperties(orderDetailsUpdateParam,orderDetails);
        orderDetails.setId(id);
        //获取数量
        BigDecimal bigDecimal = new BigDecimal(orderDetailsUpdateParam.getGoodsNum());
        //获取单价
        BigDecimal price = orderDetails.getPrice();
        //得到总价
        BigDecimal amount = price.multiply(bigDecimal);
        orderDetails.setAmount(amount);
        int rows = orderDetailsRepository.update(orderDetails);
        if(rows!=1){
            String message = "修改订单详情失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

    @Override
    public OrderDetailsStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询订单详情】的业务，ID：{}", id);
        OrderDetailsStandardVO queryResult = orderDetailsRepository.getStandardById(id);
        if (queryResult==null) {
            String message = "根据ID查询订单详情失败，订单详情数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<OrderDetailsListVO> list(Integer pageNum) {
        PageData<OrderDetailsListVO> pageData = orderDetailsRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<OrderDetailsListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询订单列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<OrderDetailsListVO> pageData = orderDetailsRepository.list(pageNum, pageSize);
        return pageData;
    }


}
